package com.sunjy.demo1.controller;

import com.alicp.jetcache.anno.CacheInvalidate;
import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.CacheUpdate;
import com.alicp.jetcache.anno.Cached;
import com.sunjy.common.annotation.BeanValid;
import com.sunjy.common.base.BaseController;
import com.sunjy.common.domain.ApiResponseT;
import com.sunjy.demo1.form.UserForm;
import com.sunjy.demo1.model.TestName;
import com.sunjy.demo1.service.TestServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @Auther: sunjy
 * @Date: 2018/10/18 10:54
 * @Description:
 */
@RestController
@RequestMapping("cache")
public class TestCacheController extends BaseController {

    @Autowired
    private TestServices testServices;

    /**
     * 设置缓存 GET
     * @param id
     * @param name
     * @return
     */
    @BeanValid
    @GetMapping(value = "/set",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Cached(name="UserService.put",cacheType = CacheType.BOTH,key="#id+#name", expire = 3600)
    public TestName putCache(Integer id, String name){

        TestName test=new TestName();
        test.setId(2);
        test.setName("get设置缓存成功");
        return test;

    }

    /**
     * 设置缓存 post
     * @param testName
     * @param bind
     * @return
     */
    @BeanValid
    @PostMapping(value = "/post",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Cached(name="UserService.setUser",cacheType = CacheType.BOTH,key="#testName.id+#testName.name", expire = 3600)
    public TestName putCache(@Valid @RequestBody UserForm testName, BindingResult bind){
        TestName test=new TestName();
        test.setId(1);
        test.setName("post设置缓存成功");
        return test;

    }

    /**
     * 删除缓存
     * @param testName
     * @param bind
     * @return
     */
    @BeanValid
    @PostMapping(value = "/delete",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @CacheInvalidate(name="UserService.setUser",key="#testName.id+#testName.name")
    public ApiResponseT CacheInvalidate(@Valid @RequestBody UserForm testName, BindingResult bind){
        return ApiResponseT.success();

    }

    /**
     * 更新缓存
     * @param testName
     * @param bind
     * @return
     */
    @BeanValid
    @PostMapping(value = "/update",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @CacheUpdate(name="UserService.setUser",key="#testName.id+#testName.name",value="#test")
    public ApiResponseT CacheUpdate(@Valid @RequestBody UserForm testName, TestName test,BindingResult bind){
        test.setId(200);
        test.setName("跟新缓存成功1");
        return ApiResponseT.success();

    }

    /**
     * 设置缓存 services层添加
     * @param userForm
     * @param bind
     * @return
     */
    @BeanValid
    @PostMapping(value = "/postServices",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponseT testServicesCache(@Valid @RequestBody UserForm userForm, BindingResult bind){
        return testServices.testCache(userForm);

    }


}
